package com.fangmao.jetpack;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.fangmao.jetpack.databinding.ActivityMainBinding;
import com.fangmao.jetpack.navigation.NavigationDemoActivity;
import com.fangmao.jetpack.viewmodel.MainViewModel;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;

/**
 * 该页面包括的内容如下:
 * ViewModel (SavedStateViewModel)
 * LiveData
 * DataBinding
 */
public class MainActivity extends AppCompatActivity {

    private MainViewModel mMainViewModel;
    private ActivityMainBinding mActivityMainBinding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mActivityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        mMainViewModel = ViewModelProviders.of(this, new SavedStateViewModelFactory(getApplication(), this)).get(MainViewModel.class);

        mActivityMainBinding.setViewmodel(mMainViewModel);
        mActivityMainBinding.setLifecycleOwner(this);

        mActivityMainBinding.toNavigation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(MainActivity.this, NavigationDemoActivity.class));
            }
        });

        getLifecycle().addObserver(mActivityMainBinding.dashBoard);
    }
}
